<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>公易贷利息月供</title>
    <style>
      body {
        margin: 0;
        padding: 2rem;
        font-size: 1rem;
      }
      label {
        display: block;
        font-weight: bold;
      }
      h2 {
        margin: 0 auto 1.5rem;
      }
      .fsc {
        display: flex;
        justify-content: flex-start;
        align-items: center;
      }
      .mb20 {
        margin-bottom: 20px;
      }
      .item {
        line-height: 2;
        margin-bottom: 1rem;
      }
      input,
      select {
        width: 100%;
        padding: 1rem;
        box-sizing: border-box;
      }
      .button {
        padding: 1rem;
        margin-top: 2rem;
        background-color: #f03131;
        color: #ffffff;
        text-align: center;
        border-radius: 5px;
        cursor: pointer;
      }
      .resultWrap {
        display: none;
        position: absolute;
        left: 2rem;
        right: 2rem;
        top: 2rem;
        border: 1px solid #ffd7a8;
        padding: 1.5rem;
        border-radius: 5px;
        background: #fff8f0;
      }
      .tip {
        color: #333;
        margin-top: 1rem;
      }
      .money {
        width: 20px;
      }
      .warning {
        margin-top: 2rem;
        color: #333;
      }
      .total_cost {
        color: #ff0000;
        font-weight: bold;
      }
      .hide {
        display: none;
      }
      .totalCost {
        color: red;
        margin-top: 1rem;
      }
      .close {
        position: absolute;
        right: 1rem;
        top: 1rem;
      }
      .close2 {
        display: block;
        width: 50%;
        margin: 2rem auto 0;
        padding: 1rem;
        background: #f03131;
        border-radius: 5px;
        color: #ffffff;
        text-align: center;
      }
    </style>
  </head>

  <body>
    <form action="">
      <div class="item">
        <label for="">贷款总额（万元）</label>
        <input type="text" placeholder="贷款总额最大为300万" id="totalPrice" />
      </div>
      <div class="main">
        <div class="item">
          <label>方案</label>
          <select id="fangAn">
            <option value="请选择" selected disabled>请选择</option>
            <option value="0">省利息方案（AA+合同）</option>
            <option value="1">存款月供少方案（V类合同）</option>
            <option value="2">长贷款，利息低方案（AC+合同）</option>
            <option value="3">按揭组合（按揭、AA+）</option>
          </select>
        </div>
        <div class="item isYear hide">
          <label>贷款年限</label>
          <select id="y1">
            <option value="请选择" selected disabled>请选择</option>
            <option value="0" data-value="16">16年</option>
            <option value="1" data-value="18">18年</option>
            <option value="2" data-value="20">20年</option>
            <option value="3" data-value="22">22年</option>
          </select>
        </div>
        <div class="item isCompose hide">
          <label>AA+额度比例（%）</label>
          <input type="text" placeholder="AA+比例不能低于两成" id="aaRatio" />
        </div>
        <div class="item isCompose hide">
          <label>贷款年限</label>
          <select id="y2"></select>
        </div>
        <div class="tip">
          请您根据您的自身需求选择最优贷款方案，如以上方案不能完全满足，请您联系我们（022-58586366,022-58586301）,我们为您量身定制，期待您的咨询。
        </div>
      </div>
      <div class="button">提交</div>
    </form>
    <div class="resultWrap">
      <h2>公易贷利息月供核算结果：</h2>
      <span class="close">关闭</span>
      <div class="result"></div>
      <span class="close2">关闭</span>
    </div>
    <script src="js/jquery-3.5.1.js"></script>
    <script>
      var baseUrl = "https://rhtm.jfzh.com.cn/mini/",
        // var baseUrl = "https://rht-admin-test.jfzh.com.cn/mini/",
        totalPrice = null,
        aaRatio = null,
        arrYear = [],
        before = null,
        after = null,
        index = null, //方案选择
        year = 0, //贷款年限（方案2）
        index2 = null, //贷款年限（方案2）
        year1 = null, //贷款年限（方案4）
        index3 = null, //贷款年限（方案4）
        isYear = false,
        isCompose = false,
        isShowResult = false,
        year2Html = '<option value="请选择" selected disabled>请选择</option>';

      for (let i = 5; i <= 25; i++) {
        arrYear.push(i);
      }
      arrYear.forEach((el, i) => {
        year2Html += `<option value="${i}" data-value="${el}">${el}年</option>`;
      });
      $("#y2").html(year2Html);

      $(".close,.close2").click(function () {
        $("form")[0].reset();
        $(".resultWrap").hide();
      });
      //贷款总额
      $("#totalPrice").keyup(function () {
        let val = parseInt($(this).val());
        if (val > 300) {
          alert("贷款总额最大为300万");
          $("#totalPrice").val("300");
          totalPrice = 300;
        } else {
          totalPrice = val;
        }
      });
      //AA+额度比例
      $("#aaRatio").blur(function () {
        let val = parseInt($(this).val());
        if (val < 20) {
          alert("AA+比例不能低于两成");
          $("#totalPrice").val("");
        } else {
          aaRatio = val;
        }
      });
      //方案选择
      $("#fangAn").change(function (event) {
        let val = $(this).val();

        index = val;
        if (val == 1) {
          isYear = true;
          isCompose = false;
          $(".isYear").show();
        } else if (val == 3) {
          isYear = false;
          isCompose = true;
          $(".isCompose").show();
        } else {
          isYear = false;
          isCompose = false;
          year = 0;
          $(".isYear,.isCompose").hide();
        }
      });
      $("#y1").change(function () {
        let val = $(this).val();
        year = parseInt($("#y1 option:selected").text());
        index2 = $(this).val();
        console.log(val);
        console.log(year);
        switch (val) {
          case "0":
            before = "118";
            after = "74";
            break;
          case "1":
            before = "135";
            after = "81";
            break;
          case "2":
            before = "153";
            after = "87";
            break;
          case "3":
            before = "172";
            after = "92";
            break;
        }
      });
      $("#y2").change(function () {
        let val = $(this).val();
        year = parseInt($("#y2 option:selected").text());
        index3 = val;
      });
      $(".button").click(function () {
        let datas = null,
          html = ""; //测算结果

        totalPrice = $("#totalPrice").val();

        if (!totalPrice) {
          alert("请输入贷款总额");
          return;
        }
        if (!index) {
          alert("您还没有选择方案");
          return;
        }
        if (isYear) {
          if (!index2) {
            alert("您还没有选择贷款年限");
            return;
          }
        }
        if (isCompose) {
          if (aaRatio < 20) {
            alert("AA+比例不能低于两成");
            return;
          }
          if (!index3) {
            alert("您还没有选择贷款年限");
            return;
          }
        }

        let data = {
          aaRatio: aaRatio,
          customerId: "",
          totalPrice: $("#totalPrice").val(),
          year: year,
        };
        if (index == 0) {
          // 公易贷测算AA
          datas = getData("calc/aaPlus", data);
        } else if (index == 1) {
          // 公易贷测算V
          datas = getData("calc/vType", data);
        } else if (index == 2) {
          // 公易贷测算AC
          datas = getData("calc/acPlus", data);
        } else {
          // AC+合同“成本月供”速算表
          datas = getData("calc/aaCompose", data);
        }

        html += `
                <div class="houseprice">贷款总额：
                    <span class="">￥${totalPrice}万元</span>
                </div>
                `;
        if (isCompose) {
          html += `
                <div class="houseprice"">其中AA+贷款：
                    <span class="">￥${datas.aaTotal}万元</span>
                </div>
                <div class="houseprice"" style="padding-left: 56rpx">按揭贷款
                    <span class="">￥${datas.cpTotal}万元</span>
                </div>
                `;
        }
        if (datas.heTongCost > 0) {
          html += `
                <div class="houseprice">合同服务费(不退)：
                    <span class="total_price">￥${datas.heTongCost} 元</span>
                </div>
                `;
        }
        if (datas.qianQiCunKuan) {
          html += `
                <div class="houseprice">前期存款：
                    <span class="">￥${datas.qianQiCunKuan} 元</span>
                </div>
                `;
        }

        if (datas.qiDongCunKuan) {
          html += `
                    <div class="houseprice start">
                        <span>
                            <span style="font-size: 28rpx;">合同启动存款：</span>
                            <span style="font-size: 28rpx;">(贷款结清后退还)</span>
                        </span>
                        <span class="" style="margin-left:15rpx;">￥${datas.qiDongCunKuan} 元</span>
                    </div>
                `;
        }
        html += `<div class = "totalCost"> 月还款： </div>`;
        if (isCompose) {
          html += `
                    
                    <div class = "houseprice two">
                        <span>${datas.qianqiTip}：</span>
                        <span class="">￥${datas.qianqiHuan}元</span> 
                    </div> 
                    <div class="houseprice two">
                      <span>${datas.zhongqiTip}：</span>
                      <span class="">￥${datas.zhongqiHuan} 元</span>
                    </div>
                    <div class="houseprice two">
                      <span>${datas.houqiTip}：</span>
                      <span class="">￥${datas.houqiHuan} 元</span>
                    </div>

                `;
        }

        if (datas.qianQiCost) {
          html += `<div class="houseprice two">`;
          if (index == 0) {
            html += ` <span>前期（48个月）：</span>`;
          } else if (index == 1) {
            html += `<span>前期（${before}个月）：</span>`;
          }
          if (index == 1 || index == 0) {
            html += `<span class="">￥${datas.qianQiCost}元</span>`;
          }
          if (index == 2) {
            html += `
                  <div class="tCost">
                    前期 
                    <div>
                      <div>（70个月）：</div>
                      <div class="mar-top">（6个月）：</div>
                    </div>
                    <div>
                      <div class="">￥${datas.qianQiCost}元</div>
                      <div class=" mar-top">￥${datas.qianqicosthou}元</div>
                    </div>
                  </div>
                </div>
                `;
          }
        }

        if (datas.houQiCost) {
          html += `
                <div class="houseprice two">
            `;
          if (index == 0) {
            html += `<span>后期（72个月）：</span>`;
          } else if (index == 1) {
            html += `<span>后期（${after}个月）:</span>`;
          } else if (index == 2) {
            html += `<span>后期（143个月）：</span>`;
          }
          html += `
                      <span class="total_price">￥${datas.houQiCost} 元</span>
                    </div>
                `;
        }

        if (datas.zongLiXi) {
          html += `
                    <div class="totalCost">总利息：
                      <span class="total_price">￥${datas.zongLiXi} 元</span>
                    </div>
                `;
        }

        if (index == 0) {
          html += `
                    <div class="total_price">贷款年限：可贷10年</div>
                `;
        } else if (index == 1) {
          html += `
                    <div class="total_price">贷款年限：可贷22年</div>
                    `;
        } else if (index == 2) {
          html += `
                    <div class="total_price">贷款年限：18年3个月</div>
                    `;
        } else if (index == 3) {
          html += `
                    <div class="total_price">贷款年限：AA+可贷10年</div>
                    <div class="total_price" style="padding-left: 170rpx">按揭可贷${year}年</div>
                    `;
        }
        html += `
                <div class="warning">
                      <div style="margin-top:15rpx">注：以上利息月供测算仅供参考，一切以办理时的实际状况为准。此测算中，未考虑申请借款人是否为自雇人士，且未考虑购买房屋为平房、地下室等因素。</div>
                    </div>
                `;

        $(".result").html(html);
        $(".resultWrap").show();
      });

      function getData(url, datas) {
        var result = null;
        $.ajax({
          contentType: "application/json",
          type: "post",
          url: baseUrl + url,
          cache: false,
          data: JSON.stringify(datas), //重点必须为一个变量如：data
          dataType: "json",
          async: false,
          success: function (data) {
            console.log("数据：" + url + ":" + data.data);
            if (data.code == 200) {
              result = data.data;
              isShowResult = true;
            }
          },
        });
        return result;
      }
      // 公易贷测算AA
      function getCesuanA(data) {
        var result = null;
        $.ajax({
          contentType: "application/json",
          type: "post",
          url: baseUrl + "calc/aaPlus",
          cache: false,
          data: JSON.stringify(data), //重点必须为一个变量如：data
          dataType: "json",
          async: false,
          success: function (data) {
            console.log(data.data);
            result = data.data;
          },
        });
        return result;
      }
      // 公易贷测算AC
      function getCesuanC(data) {
        var result = null;
        $.ajax({
          contentType: "application/json",
          type: "post",
          url: baseUrl + "calc/acPlus",
          cache: false,
          data: JSON.stringify(data), //重点必须为一个变量如：data
          dataType: "json",
          async: false,
          success: function (data) {
            console.log(data.data);
            result = data.data;
          },
        });
        return result;
      }
      // 公易贷测算V
      function getCesuanV(data) {
        var result = null;
        $.ajax({
          contentType: "application/json",
          type: "post",
          url: baseUrl + "calc/vType",
          cache: false,
          data: JSON.stringify(data), //重点必须为一个变量如：data
          dataType: "json",
          async: false,
          success: function (data) {
            console.log(data.data);
            result = data.data;
          },
        });
        return result;
      }
      // AC+合同“成本月供”速算表
      function getCesuanAC(data) {
        var result = null;
        $.ajax({
          contentType: "application/json",
          type: "post",
          url: baseUrl + "calc/aaCompose",
          cache: false,
          data: JSON.stringify(data), //重点必须为一个变量如：data
          dataType: "json",
          async: false,
          success: function (data) {
            console.log(data.data);
            result = data.data;
          },
        });
        return result;
      }
    </script>
  </body>
</html>
